import {
  statisticPocEnergyV1ControllerAnnualReport,
  statisticPocEnergyV1ControllerDailyReport,
  statisticPocEnergyV1ControllerMonthlyReport,
} from '@/services/ess/statisticPocEnergy';

export const dateFormatterMap = new Map<string, string>([
  ['day', 'YYYY-MM-DD'],
  ['date', 'YYYY-MM-DD'],
  ['month', 'YYYY-MM'],
  ['year', 'YYYY'],
]);
export const selectTypeMap = new Map<string, string>([
  ['day', 'date'],
  ['month', 'dateMonth'],
  ['year', 'dateYear'],
]);

export const netRequestMap = {
  date: statisticPocEnergyV1ControllerDailyReport,
  month: statisticPocEnergyV1ControllerMonthlyReport,
  year: statisticPocEnergyV1ControllerAnnualReport,
};

/**
 * 数组根据日期和areaId转为列
 * @param arr
 * @param isDate
 */
export function rowToColumns(arr: any[], isDate = false) {
  // 使用一个对象来存储日期和对应的分区数据
  const resultMap: Record<string, Record<string, any>> = {};
  arr.forEach((item) => {
    const {
      date,
      areaName,
      pocOffEnergy,
      pocOnEnergy,
      chargeTimes,
      dischargeTimes,
      reportId,
      stationId,
      startTime,
      endTime,
      stationName,
    } = item;

    // 如果 date 不存在，初始化
    if (!resultMap[date]) {
      if (!isDate) {
        resultMap[date] = {
          date: date,
          reportId,
          stationId,
          stationName,
        };
      } else {
        resultMap[date] = {
          date: date,
          reportId,
          stationId,
          stationName,
          startTime,
          endTime,
        };
      }
    }

    // 使用 areaName 作为前缀，构造新的字段名
    const areaPrefix = areaName;
    resultMap[date][`${areaPrefix}_pocOnEnergy`] = pocOnEnergy;
    resultMap[date][`${areaPrefix}_pocOffEnergy`] = pocOffEnergy;
    resultMap[date][`${areaPrefix}_chargeTimes`] = chargeTimes;
    resultMap[date][`${areaPrefix}_dischargeTimes`] = dischargeTimes;
  });

  // 将对象转换为数组
  return Object.values(resultMap);
}

type Points = {
  xaxis: string;
  yaxis: string | null;
};
type yAxisType = {
  deviceName: string | null;
  points: Points[];
  pointsValue: string | null;
  title: string | null;
};
/**
 * 获得多分区的seriesData
 * @param options
 */
export const getYaxisOptions = (options: yAxisType[]) => {
  return options.map((item: yAxisType) => {
    return {
      name: `${item.deviceName ? item.deviceName + '_' : ''}${item.title}`,
      type: 'bar',
      data: item.points.map((lItem) => lItem.yaxis),
    };
  });
};
